package org.cyz.tools.core.assembly;

import cn.hutool.core.util.ObjectUtil;
import javafx.stage.Stage;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Chenyz
 * @title: FileChooser
 * @projectName RedisGUI
 * @description: 文件选择器
 * @date 2020/10/2213:37
 */
public class FileChooser {

    private javafx.stage.FileChooser fileChooser;

    //选择器的标题
    private String title;
    //初始化目录
    private String initPath;
    //过滤器
    private Map<String,String> extensions=new HashMap<>();
    private Stage stage;

    public FileChooser setStage(Stage stage) {
        this.stage = stage;
        return this;
    }

    public FileChooser setTitle(String title) {
        this.title = title;
        return this;
    }

    public FileChooser setInitPath(String initPath) {
        this.initPath = initPath;
        return this;
    }

    public FileChooser setExtensions(Map<String, String> extensions) {
        this.extensions = extensions;
        return this;
    }

    public String builder(){
        fileChooser=new javafx.stage.FileChooser();
        fileChooser.setTitle(this.title);
        fileChooser.setInitialDirectory(new File(this.initPath));
        this.extensions.forEach((key,val)->{
            fileChooser.getExtensionFilters().add(new javafx.stage.FileChooser.ExtensionFilter(key,val));
        });
        fileChooser.getExtensionFilters().add(new javafx.stage.FileChooser.ExtensionFilter("All","*.*"));
        File file= fileChooser.showOpenDialog(this.stage);
        if(ObjectUtil.isNotEmpty(file)){
            return file.getAbsolutePath();
        }
        return null;
    }


}
